import config from '../../../config.js';
const base = config;
const app = getApp();
Page({
  data: {
    tabs: ["全部订单", "正在预约", "正在服务", "已完成"],
    activeIndex: 0,
    sliderOffset: 0,
    orderId: "",
    // 订单
    orderList: [],
    content: "",
    width: 94,

    tag: [{
      text: "服务态度好",
      value: "0",
      checked: false,
    }, {
      text: "响应时间快",
      value: "1",
      checked: false,
    }, {
      text: "内容回复精准",
      value: "2",
      checked: false,
    }, {
      text: "响应时间快",
      value: "3",
      checked: false,
    }, {
      text: "内容回复精准",
      value: "4",
      checked: false,
    }],

    exp: [{
      text: "服务态度好",
      value: "0",
      checked: false,
    }, {
      text: "响应时间快",
      value: "1",
      checked: false,
    }, {
      text: "内容回复精准",
      value: "2",
      checked: false,
    }],

    evaShow: false,
    hiddenmodalput: true,
    msg: {
      title: '温馨提示',
      content: ''
    },
    page: {
      start: 0,
      size: 10,
      isEnd: false
    }
  },

  evaFun: function() {
    this.setData({
      evaShow: true,
    })
  },

  closeEva: function() {
    this.setData({
      evaShow: false
    })
  },

  stopMp: function() {

  },

  tagSelect: function(e) {
    var tagArr = this.data.tag;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      tag: tagArr
    })
  },

  expSelect: function(e) {
    var tagArr = this.data.exp;
    var checkArr = e.detail.value;
    for (var i = 0; i < tagArr.length; i++) {
      if (checkArr.indexOf(i + "") != -1) {
        tagArr[i].checked = true;
      } else {
        tagArr[i].checked = false;
      }
    }
    this.setData({
      exp: tagArr
    })
  },


  star: function(e) {
    //console.log(e.currentTarget.offsetLeft);
    console.log(e.detail.x);
    let width = e.detail.x;
    this.setData({
      width: width,
    })
  },
  onLoad: function() {
    app.editTabBar();
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });

    this.loadData(0);


  },
  onShow: function(e) {
    var type = wx.getStorageSync("orderConfirmType");
    console.log(type);
    if (type != null && type == 1) {
      var orderId = wx.getStorageSync("orderId");
      this.data.orderId = orderId;
      if (orderId) {
        wx.navigateTo({
          url: "../orderDetail/orderDetail?id=" + orderId,
          complete: function() {
            wx.setStorageSync("orderConfirmType", null);
            wx.setStorageSync("orderId", null);
          }
        })
      }
    }

    this.listenOrderChange();
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  loadData(index) {
    var that = this;
    index = that.data.activeIndex;

    //加载动画
    wx.showLoading({
      title: '订单加载中...',
    })


    //请求订单列表
    wx.request({
      url: base.ordersGJList,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },
      data: {
        start: that.data.page.start,
        length: that.data.page.size,
        search: '',
        type: index
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200 || res.data.status == 201) {
          if (that.data.page.start != 0) {
            if (res.data.data != null && res.data.data.length != 0) {
              var arr = that.data.orderList;
              for (var i = 0; i < res.data.data.length; i++) {
                arr.push(res.data.data[i])
              }

              that.setData({
                orderList: arr
              })
            } else {
              that.setData({
                'page.isEnd': true
              })
            }
          } else {
            that.setData({
              orderList: res.data.data
            });
          }
        } else {
          wx.showToast({
            title: '订单获取失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }

        wx.hideLoading();


      },
      fail: function() {
        that.loadData();
      }

    })
  },
  roderDetail: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../orderDetail/orderDetail?id=" + id,
    })
  },

  modOrder: function(e) {
    var url = "";
    var typeId = e.currentTarget.dataset.type;
    var orderId = e.currentTarget.dataset.id;
    var carid = e.currentTarget.dataset.cid;
    console.log("carid:" + carid);
    switch (typeId) {
      case 1:
        url = "../cxOrder/cxOrder";
        break;
      case 2:
        url = "../accidentXz/accidentXz";
        break;
      case 3:
        url = "../peccancyBl/peccancyBl";
        break;
      case 4:
        url = "../repair/repair_zf";
        break;
      case 5:
        //带保养
        url = "../maintenance/maintenance";
        break;
      case 6:
        //接送机
        url = "../airport/airport";
        break;
      case 7:
        //代步车
        url = "../stepCar/stepCar";
        break;
      case 9:
        //代年检
        url = "../inspection/inspection";
        break;

    }
    url += "?orderId=" + orderId;
    if (typeId == 3) {
      url += "&id=" + carid;
    }
    url += "&canChange=0";
    console.log(url);
    wx.navigateTo({
      url: url,
    })
  },
  roderCancel: function(e) {
    var that = this;
    that.setData({
      orderId: e.currentTarget.dataset.id
    })

    that.setData({
      hiddenmodalput: !that.data.hiddenmodalput,
      'msg.title': "是否申请取消该客户订单？"
    })

    //取消订单
    /** **/
  },
  tabClick: function(e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    this.loadData(0);
  },
  confirmOrder: function(e) {
    var instance = this;
    wx.showModal({
      title: "温馨提示",
      content: "是否接受该客户订单？",
      showCancel: true,
      confirmColor: "#137AF0",
      success: function(er) {
        console.log(er);
        if (er.confirm) {
          let id = e.currentTarget.dataset.oid,
            that = this;
          wx.showLoading({
            title: '正在操作',
          })
          wx.request({
            url: base.orderConfirm,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': wx.getStorageSync("session")
            },
            data: {
              id: id
            },
            success: function(res) {
              wx.hideLoading();
              console.log(res);
              if (res.data.status == 200) {
                wx.showToast({
                  title: '接单成功',
                })
                instance.loadData(0);
              } else {
                wx.showToast({
                  title: "接单成功失败",
                  icon: "none"
                })
              }
            }
          })
        }
      }
    })
  },
  //取消按钮  
  cancel: function() {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function() {
    var that = this;
    if (that.data.content == "") {
      return false;
    }
    this.setData({
      hiddenmodalput: true
    })

    wx.request({
      url: base.orderCancelUrl,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': wx.getStorageSync("session")
      },

      data: {
        id: that.data.orderId,
        content: that.data.content,
        from: 1
      },
      method: "POST",
      success: function(res) {
        console.log(res.data);
        if (res.data.status == 200) {
          that.loadData(0);
          console.log(that);
        } else {
          wx.showToast({
            title: '订单取消失败！',
            icon: 'none',
            duration: 1500
          })
          return false;
        }

        that.setData({
          content: ""
        })
      }
    });

  },
  contentChange: function(e) {
    console.log(e.detail.value);
    this.setData({
      content: e.detail.value
    })
  },
  moreAction: function() {
    if (!this.data.page.isEnd) {
      this.setData({
        'page.start': ++this.data.page.start,

      })
      this.loadData();
    }

  },
  listenOrderChange: function() {
    var that = this;
    wx.onSocketMessage(function(res) {
      console.log('收到服务器推送订单消息内容：' + res.data)
      wx.setTabBarBadge({
        index: 2,
        text: '1'
      })

      //解析消息 
      that.loadData();

    })
  }


});